package isssr.bean.control;

import isssr.bean.entity.CurrentUser;
import isssr.bean.entity.OrganizationalGoal;
import isssr.bean.entity.User;
import isssr.bean.utility.HibernateUtility;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.context.FacesContext;

public class RetrieveGoalListByOwnerControl implements Serializable {

	
	private CurrentUser user;
	
	
	public RetrieveGoalListByOwnerControl()
	{
		System.out.println("costruttore controller");
		
		
		this.user = (CurrentUser) FacesContext.getCurrentInstance().
					getExternalContext().getSessionMap().get("currentUser");
		
		if(this.user == null)
			System.out.println("errore user bean di sessione");
		
		
	
	
	}
	
	
	public List<OrganizationalGoal> getOrganizationalGoalByUser()
	{
		List<OrganizationalGoal> goals = new ArrayList<OrganizationalGoal>();
		Integer user_id = this.user.getUser().getId();
		
		System.out.println("id user in metodo controller:\t"+user_id);
		
		User goal_owner = (User) HibernateUtility.retrieve(User.class, user_id);
		Iterator index = HibernateUtility.getGoalsByOwner(goal_owner);
		
		if(index==null)
			
			System.out.println("errore index nullo");

		
		while(index.hasNext())
		{
			System.out.println("entrato");
			
			OrganizationalGoal go = (OrganizationalGoal) index.next();
			System.out.println("title: " + go.getTitle());
			
			goals.add(go);
		}
	
		System.out.println("goal caricati: " + goals.size());
		
		
		return goals;
	}
	
	
	
}
